package com.bangdao.hu.roomba.helper;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import com.bangdao.hu.roomba.WifiSignalStrengthMap.MapGenerator;
import com.bangdao.hu.roomba.WifiSignalStrengthMap.MapPoint;

import android.net.wifi.ScanResult;
import android.util.Log;
import static com.bangdao.hu.TestBluetoothActivity.assetManager;

public  class Helper {
	static final String SERVER_IP = "172.19.16.96"; //172.19.16.30 172.19.16.96
	static final int SERVER_PORT = 12345;
	
	public static void sendPacket(String packet) {
		try {
			// Toast.makeText(this, "trying", Toast.LENGTH_LONG).show();
			Socket socket = new Socket(SERVER_IP, SERVER_PORT);
			// Toast.makeText(this, "conncted", Toast.LENGTH_LONG).show();
			PrintWriter out = new PrintWriter(new BufferedWriter(
					new OutputStreamWriter(socket.getOutputStream())), true);
			// out.println("hello, processing");
			out.println(packet);

		} catch (UnknownHostException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public static void parseMapFile() {
		String[] signal = null;
		int gCounter = 0, counter = 0, notEvent = 0;
		HashMap<String, Integer> hm = new HashMap<String, Integer>();
		HashMap<String, Integer> wifiCounter = new HashMap<String, Integer>();
		// open the file for reading
		InputStream instream;
		try {
			instream = assetManager.open("signal2.csv"); // openFileInput("signal2.csv");
			// if file the available for reading
			if (instream != null) {
				// prepare the file for reading
				InputStreamReader inputreader = new InputStreamReader(instream);
				BufferedReader buffreader = new BufferedReader(inputreader);

				String line;
				MapPoint mp;

				while ((line = buffreader.readLine()) != null) {

					signal = line.split(",");

					if (signal.length % 2 == 0) // if it is even number of elements
					{
						for (int i = 4; i < signal.length - 1; i += 2)
						{
							if (wifiCounter.containsKey(signal[i])) {
								int temp = wifiCounter.get(signal[i]);
								wifiCounter.put(signal[i], temp + 1);
							} else {
								wifiCounter.put(signal[i], 1);
							}
						}
						/*
						 * int xCor = Integer.parseInt(signal[1]); int yCor =
						 * Integer.parseInt(signal[3]); int[] cors = {xCor,
						 * yCor}; mp = new MapPoint( cors, hm); //create a map
						 * point MapGenerator.getInstance().putPoint(mp);
						 * for(int i = 0; i < signal.length; ++i) {
						 * //System.out.println(signal[i]); }
						 */
					} else {
						notEvent++;
						System.out.println("Bad Line Number: " + counter);
					}
				}
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}

//		System.out.println("Not Event is: " + notEvent);
//		for (String s : wifiCounter.keySet()) {
//			//System.out.println(s + " TIMES: " + wifiCounter.get(s));
//		}
		HashMap<String,Integer> newWifi = (HashMap<String, Integer>) wifiCounter.clone();
		for (String s : wifiCounter.keySet()) {

			if (wifiCounter.get(s) < 33) {
				newWifi.remove(s);
			}
		}
		
//		for (String s : newWifi.keySet()) {
//			System.out.println(s + " TIMES: " + newWifi.get(s));
//		}
		
		InputStream istream;
		try {
			istream = assetManager.open("signal2.csv"); // openFileInput("signal2.csv");
			// if file the available for reading
			int counter2 = 0;
			if (istream != null) {
				// prepare the file for reading
				InputStreamReader inputreader = new InputStreamReader(istream);
				BufferedReader buffreader = new BufferedReader(inputreader);

				String line;
				MapPoint mp;

				while ((line = buffreader.readLine()) != null) {
					signal = line.split(",");
					counter2++;
					if (signal.length % 2 == 0) // if it is even number of elements
					{
						HashMap<String, Integer> wifiHm = new HashMap<String, Integer>();
						for (int i = 4; i < signal.length - 1; i += 2) // point
						{
							if (newWifi.containsKey(signal[i])) {
								wifiHm.put(signal[i], Integer.parseInt(signal[i+1]));
							}
						}
						int xCor = Integer.parseInt(signal[1]); 
						int yCor = Integer.parseInt(signal[3]); 
						int[] cors = {xCor, yCor}; 
						mp = new MapPoint(cors, wifiHm); //create a map
						MapGenerator.getInstance().putPoint(mp);
						//ArrayList<MapPoint> mps = MapGenerator.getInstance().getMap();
						//System.out.println("msp is " + mps);
//						hm.clear();
//						ArrayList<MapPoint> mps = MapGenerator.getInstance().getMap();
//						System.out.println("msp is " + mps);
						//System.out.println("msp wifi is " + mps.get(0).getWifiID());
					}
				}
				
				System.out.println("*********COUNT is *******" + counter2);
				
				ArrayList<MapPoint> mps = MapGenerator.getInstance().getMap();
				
				System.out.println("**********MAP size is ************************" + mps.size());
				
				for(MapPoint mpp: MapGenerator.getInstance().getMap())
				{
					System.out.println("Map point x: " + mpp.getCoordinates()[0] + " y: " + mpp.getCoordinates()[1]);

				}
//				System.out.println("msp is " + mps);
			}

		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}

	}
	

}
